Next:
Java basic
, Previous:
System
, Up:
Index
object oriented programming
by useing Java’s object oriented programming feature,
can represent program more efficient.
by using OOP feature maken Card Company Program
Cardcompany.java
package
mkcard
;
public
class
Cardcompany
{
private
static
Cardcompany
instance
=
new
Cardcompany
();
private
Cardcompany
() {}
public
static
Cardcompany
getInstance
() {
if
(
instance
==
null
) {
instance
=
new
CardCompany
();
}
return
instance
;
}
public
Card
MkCard
(
int
money
) {
Card
card
=
new
Card
(
money
);
return
card
;
}
}
Card.java
package
mkcard
;
public
class
Card
{
private
static
int
MkCard
=
10000
;
private
int
CardNum
;
private
int
money
=
0
;
public
int
getMoney
() {
return
money
;
}
public
void
chargeMoney
(
int
money
) {
this
.
money
+=
money
;
}
public
Card
(
int
money
) {
MkCard
++
;
CardNum
=
MkCard
;
this
.
money
=
money
;
}
public
int
getCardNum
() {
return
CardNum
;
}
}
Test.java
package
mkcard
;
public
class
Test
{
public
static
void
main
(
String
[]
args
) {
Cardcompany
cardCompany
=
Cardcompany
.
getInstance
();
Card
card1
=
cardCompany
.
MkCard
(
30000
);
Card
card2
=
cardCompany
.
MkCard
(
500000
);
card1
.
chargeMoney
(
15000
);
card2
.
chargeMoney
(
150000
);
System
.
out
.
println
(
card1
.
getCardNum
());
System
.
out
.
println
(
card2
.
getCardNum
());
System
.
out
.
println
(
card1
.
getMoney
());
System
.
out
.
println
(
card2
.
getMoney
());
}
}
10001
10002
45000
650000